package test;

import dao.UserMapper;
import entity.Bill;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;
import utils.MyBatisUtil;

import java.util.ArrayList;
import java.util.List;

public class ShangJi02 {

    private Logger logger=Logger.getLogger(ShangJi01.class);

    /**
     * 添加
     */
    @Test
    public void TestAdd(){
        SqlSession sqlSession=null;
        int count=0;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            Bill bill=new Bill();
            bill.setProductName("添加测试");
            bill.setIsPayment(2);
            bill.setBillCode("添加测试码");
            count=sqlSession.getMapper(UserMapper.class).add(bill);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            count=0;
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        if (count>0){
            logger.debug("添加成功");
        }

    }


    /**
     * 修改
     */
    @Test
    public void Testmodifty(){
        SqlSession sqlSession=null;
        int count=0;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            Bill bill=new Bill();
            bill.setId(19);
            bill.setProviderId(20);
            bill.setProductName("修改测试");
            count=sqlSession.getMapper(UserMapper.class).modifty(bill);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            count=0;
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        if (count>0){
            logger.debug("修改成功");
        }
    }


    /**
     * 删除
     */
    @Test
    public void Testdelete() {
        SqlSession sqlSession = null;
        int count = 0;
        int providerId=20;
        int id=19;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(UserMapper.class).deleteByid(providerId,id);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            count = 0;
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        if (count > 0) {
            logger.debug("删除成功");
        }
    }
}
